package com.andaily.domain.dto.account;

import com.andaily.domain.account.Account;
import com.andaily.domain.account.AccountKind;
import com.andaily.domain.account.AccountType;
import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.user.User;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Shengzhao Li
 * @see com.andaily.domain.account.Account
 */
public class AccountFormDTO extends AbstractDTO {

    private AccountKind kind = AccountKind.EXPENDITURE;
    private AccountTypeDTO accountType;

    private String amount;
    private String accountDate;
    private String description;

    /**
     * If the value is 'true', it's mean go on adding.
     */
    private boolean addNext;

    public AccountFormDTO() {
        this.accountDate = DateUtils.toDateText(DateUtils.now());
        this.accountType = new AccountTypeDTO();
    }

    public AccountFormDTO(Account account) {
        this.guid = account.guid();
        this.kind = account.kind();
        this.amount = account.amount().toString();

        this.accountDate = DateUtils.toDateText(account.accountDate());
        this.description = account.description();
        this.accountType = new AccountTypeDTO(account.type());
    }

    public AccountKind getKind() {
        return kind;
    }

    public void setKind(AccountKind kind) {
        this.kind = kind;
    }

    public AccountTypeDTO getAccountType() {
        return accountType;
    }

    public void setAccountType(AccountTypeDTO accountType) {
        this.accountType = accountType;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getAccountDate() {
        return accountDate;
    }

    public void setAccountDate(String accountDate) {
        this.accountDate = accountDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isAddNext() {
        return addNext;
    }

    public void setAddNext(boolean addNext) {
        this.addNext = addNext;
    }

    public Account toAccount(AccountType type, User owner) {
        BigDecimal accountAmount = new BigDecimal(this.amount);
        Date date = DateUtils.getDate(this.accountDate);

        Account account = new Account(accountAmount, type, date, description);
        if (type.isIncome()) {
            account.kind(AccountKind.INCOME);
        }
        return account.owner(owner);
    }

    public void updateDate(String date) {
        if (StringUtils.isNotEmpty(date)) {
            this.accountDate = date;
        }
    }

    public void updateTypeGuid(String typeGuid) {
        if (StringUtils.isNotEmpty(typeGuid) && (accountType == null || (StringUtils.isEmpty(accountType.getGuid())))) {
            this.accountType = new AccountTypeDTO();
            this.accountType.setGuid(typeGuid);
        }
    }
}
